﻿/**
* Interface Elements for jQuery
* Fisheye menu
* 
* http://interface.eyecon.ro
* 
* Copyright (c) 2006 Stefan Petre
* Dual licensed under the MIT (MIT-LICENSE.txt) 
* and GPL (GPL-LICENSE.txt) licenses.
*
*/

/**
* Build a Fisheye menu from a list of links
*
* @name Fisheye
* @description Build a Fisheye menu from a list of links
* @param Hash hash A hash of parameters
* @option String items items selection
* @option String container container element
* @option Integer itemWidth the minimum width for each item
* @option Integer maxWidth the maximum width for each item
* @option String itemsText selection of element that contains the text for each item
* @option Integer proximity the distance from element that make item to interact
* @option String valign vertical alignment
* @option String halign horizontal alignment
*
* @type jQuery
* @cat Plugins/Interface
* @author Stefan Petre
*/

jQuery.fn.slideDownDD = function (speed, callback) { jQuery.fn.animate({ 'show': 1 }, speed, 'linear', callback); }
jQuery.iFisheye = {
    build: function (options) {

        return this.each(
			function () {
			    var el = this;
			    el.fisheyeCfg = {
			        items: jQuery(options.items, this),
			        container: jQuery(options.container, this),
			        pos: jQuery.iUtil.getPosition(this),
			        itemWidth: options.itemWidth,
			        itemsText: options.itemsText,
			        proximity: options.proximity,
			        valign: options.valign,
			        halign: options.halign,
			        maxWidth: options.maxWidth,
			        dropMenuSelector: options.dropMenu,
			        dropMenuItemSelector: options.dropMenuItem,
			        dropMenus: jQuery(options.dropMenu, this),
			        dropMenuRadius: options.dropMenuRadius,
			        radiusDirection: options.radiusDirection,
			        shownMenu: null,
			        mouseover_tid: [],
			        mouseout_tid: []
			    };
			    jQuery.iFisheye.positionContainer(el, 0);
			    jQuery(window).bind(
					'resize',
					function () {
					    el.fisheyeCfg.pos = jQuery.iUtil.getPosition(el);
					    jQuery.iFisheye.positionContainer(el, 0);
					    jQuery.iFisheye.positionItems(el);
					}
				);
			    jQuery.iFisheye.positionItems(el);
			    el.fisheyeCfg.items
                    .each(function (index) {
                        jQuery(this).hover(
                            function (e) {
                                //                                clearTimeout(el.fisheyeCfg.mouseout_tid[index]);
                                //                                el.fisheyeCfg.mouseover_tid[index] = setTimeout(
                                //                                    function () {
                                jQuery(el.fisheyeCfg.dropMenuSelector, this).stop(false, true).slideDown(1000, function () { el.fisheyeCfg.shownMenu = this; });
                                //                                    }
                                //                                    , 1200
                                //                                );
                            },
                            function (e) {
                                //                                clearTimeout(el.fisheyeCfg.mouseover_tid[index]);
                                //                                el.fisheyeCfg.mouseout_tid[index] = setTimeout(
                                //                                    function () {
                                jQuery(el.fisheyeCfg.dropMenuSelector, this).stop(false, true).slideUp(1000, function () { });
                                //                                    }
                                //                                    , 1200);
                            }
                        );
                    });

			    el.fisheyeCfg.dropMenus
                    .hover(
                        function (e) {
                            el.fisheyeCfg.shownMenu = this;
                        },
                        function (e) {
                            el.fisheyeCfg.shownMenu = null;
                        }
                    );
			    jQuery(document)
                    .mousemove(
					    function (e) {
					        e = e || window.event;
					        var target = e.srcElement || e.target;
					        var pointer = jQuery.iUtil.getPointer(e);
					        var toAdd = 0;
					        if (el.fisheyeCfg.halign && el.fisheyeCfg.halign == 'center')
					            var posx = pointer.x - el.fisheyeCfg.pos.x - (el.offsetWidth - el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size()) / 2 - el.fisheyeCfg.itemWidth / 2;
					        else if (el.fisheyeCfg.halign && el.fisheyeCfg.halign == 'right')
					            var posx = pointer.x - el.fisheyeCfg.pos.x - el.offsetWidth + el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size();
					        else
					            var posx = pointer.x - el.fisheyeCfg.pos.x;
					        var posy = Math.pow(pointer.y - el.fisheyeCfg.pos.y - el.offsetHeight / 2, 2);
					        if (el.fisheyeCfg.shownMenu && jQuery(el.fisheyeCfg.shownMenu).css('display') == 'block') {
					            posy = 0;
					        }
					        //var posy = Math.pow(pointer.y - el.fisheyeCfg.pos.y, 2);
					        el.fisheyeCfg.items.each(
							    function (nr) {
							        distance = Math.sqrt(
									    Math.pow(posx - nr * el.fisheyeCfg.itemWidth, 2)
									    + posy
								    );
							        distance -= el.fisheyeCfg.itemWidth / 2;

							        distance = distance < 0 ? 0 : distance;
							        distance = distance > el.fisheyeCfg.proximity ? el.fisheyeCfg.proximity : distance;
							        distance = el.fisheyeCfg.proximity - distance;

							        extraWidth = el.fisheyeCfg.maxWidth * distance / el.fisheyeCfg.proximity;

							        this.style.width = el.fisheyeCfg.itemWidth + extraWidth + 'px';
							        this.style.left = el.fisheyeCfg.itemWidth * nr + toAdd + 'px';
							        toAdd += extraWidth;

							        var itemTop = 0;
							        var hdirection = (el.fisheyeCfg.radiusDirection && el.fisheyeCfg.radiusDirection == 'right') ? 1 : -1;
							        var vdirection = (el.fisheyeCfg.valign && el.fisheyeCfg.valign == 'bottom') ? -1 : 1;
							        var dropMenuItems = jQuery(el.fisheyeCfg.dropMenuItemSelector, this);
							        var length = dropMenuItems.size();
							        dropMenuItems.each(function (mr) {
							            var count = vdirection == -1 ? (length - (mr - 1)) : (mr + 1);
							            var dis = Math.pow(el.fisheyeCfg.dropMenuRadius, 2) - (count == 0 ? 0 : Math.pow(el.fisheyeCfg.itemWidth * count, 2));
							            var padding = (el.fisheyeCfg.dropMenuRadius - Math.sqrt(dis));
							            padding = hdirection > 0 ? padding : (-1) * padding;
							            var paddingDirection = hdirection > 0 ? 'padding-left' : 'padding-right';
							            itemTop += vdirection * (el.fisheyeCfg.itemWidth + extraWidth);
							            $(this)
                                               .css('width', el.fisheyeCfg.itemWidth + extraWidth + 'px')
                                               .css("top", itemTop + 'px')
                                               .css('padding-left', 300 + Math.abs(padding) + 'px');

							            //$(this).find('a').css('left', paddingLeft + 'px').css("top", itemTop + 'px')
							        });
							    }
						    );
					        jQuery.iFisheye.positionContainer(el, toAdd);
					    }
				);
			}
		)
    },

    positionContainer: function (el, toAdd) {
        if (el.fisheyeCfg.halign)
            if (el.fisheyeCfg.halign == 'center')
                el.fisheyeCfg.container.get(0).style.left = (el.offsetWidth - el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size()) / 2 - toAdd / 2 + 'px';
            else if (el.fisheyeCfg.halign == 'left')
                el.fisheyeCfg.container.get(0).style.left = -toAdd / el.fisheyeCfg.items.size() + 'px';
            else if (el.fisheyeCfg.halign == 'right')
                el.fisheyeCfg.container.get(0).style.left = (el.offsetWidth - el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size()) - toAdd / 2 + 'px';
        el.fisheyeCfg.container.get(0).style.width = el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size() + toAdd + 'px';
    },

    positionItems: function (el) {
        el.fisheyeCfg.items.each(
			function (nr) {
			    this.style.width = el.fisheyeCfg.itemWidth + 'px';
			    this.style.left = el.fisheyeCfg.itemWidth * nr + 'px';
			}
		);
    }
};

jQuery.fn.Fisheye = jQuery.iFisheye.build;